<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_job2dao/model')->方法名();
*/
namespace jy_job2dao;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function iunserializer($value) {
		if (empty($value)) {
			return array();
		}
		if (!$this->is_serialized($value)) {
			return $value;
		}
		if (version_compare(PHP_VERSION, '7.0.0', '>=')) {
			$result = unserialize($value, array('allowed_classes' => false));
		} else {
			if (preg_match('/[oc]:[^:]*\d+:/i', $value)) {
				return array();
			}
			$result = unserialize($value);
		}
		if (false === $result) {
			$temp = preg_replace_callback('!s:(\d+):"(.*?)";!s', function ($matchs) {
				return 's:' . strlen($matchs[2]) . ':"' . $matchs[2] . '";';
			}, $value);

			return unserialize($temp);
		} else {
			return $result;
		}
	}
	function is_serialized($data, $strict = true) {
		if (!is_string($data)) {
			return false;
		}
		$data = trim($data);
		if ('N;' == $data) {
			return true;
		}
		if (strlen($data) < 4) {
			return false;
		}
		if (':' !== $data[1]) {
			return false;
		}
		if ($strict) {
			$lastc = substr($data, -1);
			if (';' !== $lastc && '}' !== $lastc) {
				return false;
			}
		} else {
			$semicolon = strpos($data, ';');
			$brace = strpos($data, '}');
					if (false === $semicolon && false === $brace) {
				return false;
			}
					if (false !== $semicolon && $semicolon < 3) {
				return false;
			}
			if (false !== $brace && $brace < 4) {
				return false;
			}
		}
		$token = $data[0];
		switch ($token) {
			case 's':
				if ($strict) {
					if ('"' !== substr($data, -2, 1)) {
						return false;
					}
				} elseif (false === strpos($data, '"')) {
					return false;
				}
							case 'a':
				return (bool) preg_match("/^{$token}:[0-9]+:/s", $data);
			case 'O':
				return false;
			case 'b':
			case 'i':
			case 'd':
				$end = $strict ? '$' : '';

				return (bool) preg_match("/^{$token}:[0-9.E-]+;$end/", $data);
		}

		return false;
	}
}
?>